<!--
BSD LICENSE

Copyright(c) 2022 Intel Corporation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.
  * Neither the name of Intel Corporation nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.
    
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.-->

<mat-card class="pool-config">
  <mat-card-title>Resource Pool Configuration</mat-card-title>
  <mat-card-content>
    <div class="info-text">
      Resource pools are groups of cores with a fixed amount of processor
      resources, such as cache and memory bandwidth. Resource pools can be
      configured to isolate or restrict resources available to applications
      assigned to the pool.
    </div>
    <div class="config">
      <mat-form-field appearance="fill">
        <mat-label>Select a Pool</mat-label>
        <mat-select [(value)]="selected">
          <mat-option
            *ngFor="let pool of pools"
            [value]="pool.name"
            (onSelectionChange)="selectedPool($event, pool.id)"
          >
            {{ pool.name }}
          </mat-option>
        </mat-select>
      </mat-form-field>
      <div class="action-buttons">
        <button
          mat-mini-fab
          color="primary"
          id="add-pool-button"
          (click)="poolAddDialog()"
        >
          <mat-icon>add</mat-icon>
        </button>
        <button
          mat-mini-fab
          color="accent"
          id="delete-pool-button"
          (click)="deletePool()"
        >
          <mat-icon>delete</mat-icon>
        </button>
      </div>
    </div>

    <div class="pool-details" *ngIf="pool">
      <div class="pool-name">
        <mat-form-field
          class="name-field"
          appearance="fill"
          floatLabel="always"
        >
          <mat-label>Name of Pool</mat-label>
          <input matInput [formControl]="nameControl" required />
          <mat-error *ngIf="nameControl.errors?.['pattern']"
            >name has invalid characters</mat-error
          >
          <mat-error *ngIf="nameControl.errors?.['required']"
            >name is required
          </mat-error>
          <mat-error *ngIf="nameControl.hasError('maxlength')"
            >max length 80 characters</mat-error
          >
        </mat-form-field>
        <button
          mat-raised-button
          color="primary"
          id="rename-pool-button"
          (click)="savePoolName()"
          [disabled]="!nameControl.valid"
        >
          RENAME
        </button>
      </div>
      <span *ngIf="pool?.l3cbm || l3cat?.cdp_enabled"
        >L3 Cache Allocation Technology (L3 CAT)</span
      >
      <div class="pool-cbm" *ngIf="pool.l3cbm && !l3cat?.cdp_enabled">
        <div class="cbm">
          <button
            mat-flat-button
            disableRipple="true"
            class="cbm-button"
            *ngFor="let bit of pool?.l3Bitmask; index as i"
            [style.color]="bit ? 'black' : 'grey'"
            [style.borderColor]="bit ? 'black' : 'lightgrey'"
            [style.background]="bit ? '#f0f0f0' : 'white'"
            (click)="onChangeL3CBM(bit, i)"
          >
            {{ bit }}
          </button>
        </div>
        <button
          mat-raised-button
          color="primary"
          id="save-l3cbm-button"
          (click)="saveL3CBM()"
        >
          APPLY
        </button>
      </div>
      <div class="pool-cbm" *ngIf="l3cat?.cdp_enabled">
        <div class="cbm-label">
          <div class="cbm-code">Code</div>
          <div class="cbm-data">Data</div>
        </div>
        <div>
          <div class="cbm">
            <button
              mat-flat-button
              disableRipple="true"
              class="cbm-button"
              *ngFor="let bit of pool?.l3BitmaskCode; index as i"
              [style.color]="bit ? 'black' : 'grey'"
              [style.borderColor]="bit ? 'black' : 'lightgrey'"
              [style.background]="bit ? '#f0f0f0' : 'white'"
              (click)="onChangeL3CdpCode(bit, i)"
            >
              {{ bit }}
            </button>
          </div>
          <br />
          <div class="cbm">
            <button
              mat-flat-button
              disableRipple="true"
              class="cbm-button"
              *ngFor="let bit of pool?.l3BitmaskData; index as i"
              [style.color]="bit ? 'black' : 'grey'"
              [style.borderColor]="bit ? 'black' : 'lightgrey'"
              [style.background]="bit ? '#f0f0f0' : 'white'"
              (click)="onChangeL3CdpData(bit, i)"
            >
              {{ bit }}
            </button>
          </div>
        </div>
        <div>
          <button
            mat-raised-button
            color="primary"
            id="save-l3cdp-button"
            (click)="saveL3CBM()"
          >
            APPLY
          </button>
        </div>
      </div>
      <span *ngIf="pool.l2cbm || l2cat?.cdp_enabled"
        >L2 Cache Allocation Technology (L2 CAT)</span
      >
      <div class="pool-cbm" *ngIf="pool.l2cbm && !l2cat?.cdp_enabled">
        <div class="cbm">
          <button
            mat-flat-button
            disableRipple="true"
            class="cbm-button"
            *ngFor="let n of pool.l2Bitmask; index as i"
            [style.color]="n ? 'black' : 'grey'"
            [style.borderColor]="n ? 'black' : 'lightgrey'"
            [style.background]="n ? '#f0f0f0' : 'white'"
            (click)="onChangeL2CBM(n, i)"
          >
            {{ n }}
          </button>
        </div>
        <button
          mat-raised-button
          id="save-l2cbm-button"
          color="primary"
          (click)="saveL2CBM()"
        >
          APPLY
        </button>
      </div>

      <div class="pool-cbm" *ngIf="l2cat?.cdp_enabled">
        <div class="cbm-label">
          <div class="cbm-code">Code</div>
          <div class="cbm-data">Data</div>
        </div>
        <div>
          <div class="cbm">
            <button
              mat-flat-button
              disableRipple="true"
              class="cbm-button"
              *ngFor="let bit of pool?.l2BitmaskCode; index as i"
              [style.color]="bit ? 'black' : 'grey'"
              [style.borderColor]="bit ? 'black' : 'lightgrey'"
              [style.background]="bit ? '#f0f0f0' : 'white'"
              (click)="onChangeL2CdpCode(bit, i)"
            >
              {{ bit }}
            </button>
          </div>
          <br />
          <div class="cbm">
            <button
              mat-flat-button
              disableRipple="true"
              class="cbm-button"
              *ngFor="let bit of pool?.l2BitmaskData; index as i"
              [style.color]="bit ? 'black' : 'grey'"
              [style.borderColor]="bit ? 'black' : 'lightgrey'"
              [style.background]="bit ? '#f0f0f0' : 'white'"
              (click)="onChangeL2CdpData(bit, i)"
            >
              {{ bit }}
            </button>
          </div>
        </div>
        <div>
          <button
            mat-raised-button
            color="primary"
            id="save-l2cdp-button"
            (click)="saveL2CBM()"
          >
            APPLY
          </button>
        </div>
      </div>
      <span *ngIf="pool.mba || pool.mba_bw"
        >Memory Bandwidth Allocation (MBA)</span
      >
      <div class="pool-mba" *ngIf="pool.mba">
        <div class="pool-mba">
          <mat-slider
            thumbLabel
            [value]="pool.mba"
            min="10"
            max="100"
            step="10"
            aria-label="units"
            (change)="onChangeMBA($event)"
          ></mat-slider>
        </div>
        <button
          mat-raised-button
          color="primary"
          id="save-mba-button"
          (click)="saveMBA()"
        >
          APPLY
        </button>
      </div>
      <div class="pool-mba-mbps" *ngIf="pool.mba_bw">
        <mat-form-field appearance="fill" floatLabel="always">
          <mat-label>MBps</mat-label>
          <input
            matInput
            [formControl]="mbaBwControl"
            [placeholder]="
              pool.mba_bw === mbaBwDefNum || pool.mba_bw ? 'Unrestricted' : ''
            "
            required
            type="number"
          />
          <mat-error *ngIf="mbaBwControl.errors?.['required']"
            >MBps is required
          </mat-error>
          <mat-error
            *ngIf="mbaBwControl.errors?.['min'] || mbaBwControl.errors?.['max'] || mbaBwControl.errors?.['pattern']"
            >MBps value too large
          </mat-error>
        </mat-form-field>
        <div class="action-btn">
          <button
            mat-raised-button
            color="accent"
            id="mbabw-reset-button"
            (click)="saveMBABW(true)"
          >
            Reset
          </button>
          <button
            mat-raised-button
            color="primary"
            id="mbabw-apply-button"
            (click)="saveMBABW(false)"
            [disabled]="!mbaBwControl.valid"
          >
            APPLY
          </button>
        </div>
      </div>
      <div *ngIf="caps.includes('power') && !sstbf?.configured" >
        <span>Speed Select Technology - Core Power (SST-CP)</span>
        <br />
        <div class="none-text" *ngIf="!profiles.length">No profiles are currently configured</div>
        <div class="profile" *ngIf="profiles.length">
          <mat-form-field appearance="fill">
            <mat-label>Select a Power Profile</mat-label>
            <mat-select
              [(value)]="profileID === undefined ? '' : profileID"
              [formControl]="profileControl"
            >
              <mat-option *ngFor="let profile of profiles" [value]="profile.id">
                {{ profile.name }}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <button
            mat-raised-button
            color="primary"
            (click)="savePowerProfile()"
          >
            APPLY
          </button>
        </div>
      </div>
      <br *ngIf="!profiles.length"/>
      <span>Apps</span>
      <br/>
      <div class="none-text" *ngIf="!poolApps.length">No apps currently assigned to pool</div>
      <div class="pool-apps">
        <button
          mat-stroked-button
          color="primary"
          *ngFor="let poolApp of poolApps"
        >
          {{ poolApp.name }}
        </button>
      </div>
      <span *ngIf="pool.cores.length">Cores</span>
      <div class="cores">
        <div class="pool-cores" *ngIf="pool.cores.length">
          <button
            class="core-button"
            mat-stroked-button
            *ngFor="let core of pool.cores"
          >
            {{ core }}
          </button>
        </div>
        <button
          mat-raised-button
          color="primary"
          id="edit-cores-button"
          (click)="coresEditDialog()"
        >
          EDIT
        </button>
      </div>
    </div>
  </mat-card-content>
</mat-card>
